package com.mall.shopping.services;

import com.mall.commons.tool.exception.ValidateException;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class CartServiceImpl implements ICartService {
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    ItemMapper itemMapper;
    String useStr = "cart_userId_";

    @Override
    public CartListByIdResponse getCartListById(CartListByIdRequest request) {
        CartListByIdResponse cartListByIdResponse = new CartListByIdResponse();
        try {
            request.requestCheck();
        } catch (ValidateException e) {
            cartListByIdResponse.setCode(e.getErrorCode());
            cartListByIdResponse.setMsg(e.getMessage());
            return cartListByIdResponse;
        }
        String userId = useStr + request.getUserId().toString();
        RMap<Long, CartProductDto> cartList = redissonClient.getMap(userId);
        Collection<CartProductDto> cartProductDtos = cartList.readAllValues();
        cartListByIdResponse.setCartProductDtos((List<CartProductDto>) cartProductDtos);
        cartListByIdResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        cartListByIdResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return cartListByIdResponse;
    }

    @Override
    public AddCartResponse addToCart(AddCartRequest request) {
        AddCartResponse addCartResponse = new AddCartResponse();
        try {
            request.requestCheck();
        } catch (ValidateException e) {
            addCartResponse.setCode(e.getErrorCode());
            addCartResponse.setMsg(e.getMessage());
            return addCartResponse;
        }
        String userId = useStr + request.getUserId().toString();
        RMap<Long, CartProductDto> map = redissonClient.getMap(userId);

        if (map.containsKey(request.getProductId())) {
            CartProductDto cartProductDto = map.get(request.getProductId());
            Long productNum = cartProductDto.getProductNum();
            cartProductDto.setProductNum(productNum + request.getProductNum());
            map.put(request.getProductId(), cartProductDto);
        } else {
            Item item = itemMapper.selectByPrimaryKey(request.getProductId());
            CartProductDto cartProductDto = new CartProductDto();
            cartProductDto.setProductNum(request.getProductNum());
            cartProductDto.setProductId(request.getProductId());
            cartProductDto.setLimitNum(item.getLimitNum().longValue());
            cartProductDto.setProductImg(item.getImage());
            cartProductDto.setSalePrice(item.getPrice());
            cartProductDto.setProductName(item.getTitle());

            map.put(request.getProductId(), cartProductDto);
        }
        addCartResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        addCartResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        return addCartResponse;
    }

    @Override
    public UpdateCartNumResponse updateCartNum(UpdateCartNumRequest request) {
        UpdateCartNumResponse updateCartNumResponse = new UpdateCartNumResponse();
        try {
            request.requestCheck();
        } catch (ValidateException e) {
            updateCartNumResponse.setCode(e.getErrorCode());
            updateCartNumResponse.setMsg(e.getMessage());
            return updateCartNumResponse;
        }
        String userId = useStr + request.getUserId().toString();
        RMap<Long, CartProductDto> map = redissonClient.getMap(userId);
        map.get(request.getProductId()).setChecked(request.getChecked());
        map.get(request.getProductId()).setProductNum(Long.parseLong(request.getProductNum().toString()));

        updateCartNumResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        updateCartNumResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return updateCartNumResponse;
    }

    @Override
    public CheckAllItemResponse checkAllCartItem(CheckAllItemRequest request) {
        return null;
    }

    @Override
    public DeleteCartItemResponse deleteCartItem(DeleteCartItemRequest request) {
        DeleteCartItemResponse deleteCartItemResponse = new DeleteCartItemResponse();
        try {
            request.requestCheck();
        } catch (ValidateException e) {
            deleteCartItemResponse.setCode(e.getErrorCode());
            deleteCartItemResponse.setMsg(e.getMessage());
            return deleteCartItemResponse;
        }
        RMap<Object, Object> map = redissonClient.getMap(useStr + request.getUserId().toString());
        map.remove(request.getItemId());

        deleteCartItemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        deleteCartItemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return deleteCartItemResponse;
    }

    @Override
    public DeleteCheckedItemResposne deleteCheckedItem(DeleteCheckedItemRequest request) {

        DeleteCheckedItemResposne deleteCheckedItemResposne = new DeleteCheckedItemResposne();
        try {
            request.requestCheck();
        } catch (ValidateException e) {
            deleteCheckedItemResposne.setCode(e.getErrorCode());
            deleteCheckedItemResposne.setMsg(e.getMessage());
            return deleteCheckedItemResposne;
        }
        RMap<Long, CartProductDto> map = redissonClient.getMap(useStr + request.getUserId().toString());
        Set keys = map.readAllKeySet();
        for (Object key : keys) {
            if (!map.get(key).getChecked().equals("false")) {
                map.remove(key);
            }
        }
        deleteCheckedItemResposne.setCode(ShoppingRetCode.SUCCESS.getCode());
        deleteCheckedItemResposne.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return deleteCheckedItemResposne;
    }

    @Override
    public ClearCartItemResponse clearCartItemByUserID(ClearCartItemRequest request) {
        return null;
    }
}